package com.ruoyi.resume.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.resume.domain.model.match.JobMatchVo;
import com.ruoyi.resume.domain.model.SplitResumeResult;
import com.ruoyi.resume.domain.response.SplitResumeResultResp;
import com.ruoyi.resume.service.FirstLabelingService;
import com.ruoyi.resume.service.UploadResumeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Api(tags = "简历上传与解析")
@RestController
@RequestMapping("/resume")
public class UploadResumeController {

    @Autowired
    private UploadResumeService uploadResumeService;

    @Autowired
    private FirstLabelingService firstLabelingService;

    /**
     * 上传简历
     *
     * @author 张涵姣
     */
// 上传简历并处理
    @ApiOperation("上传简历")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "简历文件", dataType = "MultipartFile", paramType = "body")
    })
    @PostMapping("/upload-resume")
    @Anonymous
    public ResponseEntity<SplitResumeResultResp> uploadResume(@RequestParam("file") MultipartFile file) {
        return ResponseEntity.ok(uploadResumeService.processResume(file));
    }


    /**
     * 保存简历信息（前端编辑保存后的）
     *
     * @author 田成强
     */
    @ApiOperation("保存编辑后的简历信息")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "resume",
                    value = "结构化简历数据",
                    required = true,
                    dataType = "SplitResumeResultResp",
                    paramType = "body"
            )
    })
    @PostMapping("/save-resume")
    @Anonymous
    public ResponseEntity<?> saveResume(@RequestBody SplitResumeResult resume) {
        try {
            // 调用服务层保存简历
            SplitResumeResult savedResume = uploadResumeService.saveStructuredResume(resume);

            // 返回保存成功的简历数据
            return ResponseEntity.ok(Collections.singletonMap("data", savedResume));
        } catch (IllegalArgumentException e) {
            // 处理参数校验异常
            return ResponseEntity.badRequest()
                    .body(Collections.singletonMap("error", e.getMessage()));
        } catch (Exception e) {
            // 处理其他异常
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Collections.singletonMap("error", "简历保存失败: " + e.getMessage()));
        }
    }

    /**
     * 简历标签初始化
     * @param request 简历所有信息对象
     * @return maplist
     */
    @ApiOperation(value = "生成初始简历所有标签")
    @PostMapping("/first-labeling")
    @Anonymous
    public ResponseEntity<List<Map<String, Integer>>> generateHometownLabel(
            @ApiParam(value = "简历所有信息", required = true)
            @RequestBody SplitResumeResult request) {

        // 调用服务层生成籍贯标签
        List<Map<String, Integer>> result = firstLabelingService.firstLabeling(request);

        // 返回标签键值对列表
        return ResponseEntity.ok(result);
    }


    /**
     * 根据简历 ID 生成所有关联标签
     * @param resumeId 简历 ID
     * @return 标签键值对列表
     */
    @ApiOperation(value = "根据简历 ID 生成所有标签")
    @PostMapping("/label-by-id")
    @Anonymous
    public ResponseEntity<List<Map<String, Integer>>> generateLabelsByResumeId(
            @ApiParam(value = "简历 ID", required = true)
            @RequestParam Integer resumeId) {

        // 通过服务层调用方法生成标签
        List<Map<String, Integer>> result = firstLabelingService.generateLabelsForResume(resumeId);

        // 返回标签键值对列表
        return ResponseEntity.ok(result);
    }


    /**
     * 上传并解析存储简历
     *
     * @author 张涵姣
     */
    @ApiOperation("上传并解析存储简历")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "简历文件", dataType = "MultipartFile", paramType = "body")
    })
    @PostMapping("/upload-parse-store")
    @Anonymous
    public ResponseEntity<?> uploadParseStoreResume(@RequestParam("file") MultipartFile file) {
        try {
            // 上传并解析简历
            SplitResumeResultResp splitResumeResultResp = uploadResumeService.processResume(file);
            SplitResumeResult parsedResume = splitResumeResultResp.getSplitResumeResult();

            if("女".equals(parsedResume.getBasicInfo().getGender())) {
                parsedResume.getBasicInfo().setGender("F");
            }else if("男".equals(parsedResume.getBasicInfo().getGender())){
                parsedResume.getBasicInfo().setGender("M");
            } else{
                parsedResume.getBasicInfo().setGender("O");
            }

            // 存储解析后的简历数据
            SplitResumeResult savedResume = uploadResumeService.saveStructuredResume(parsedResume);

            // 返回存储成功的结果
            return ResponseEntity.ok(Collections.singletonMap("data", savedResume));
        } catch (IllegalArgumentException e) {
            // 处理参数校验异常
            return ResponseEntity.badRequest()
                    .body(Collections.singletonMap("error", e.getMessage()));
        } catch (Exception e) {
            // 处理其他异常
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Collections.singletonMap("error", "简历上传、解析或存储失败: " + e.getMessage()));
        }
    }


}